package com.zd.kids.common;

import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.zd.kids.R;

import androidx.annotation.DrawableRes;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * @ClassName: com.hyy.supermarket.common
 * @Description:
 * @Author: hyy
 * @Date: 2020/5/8
 * @Time: 3:15 PM
 */
public class GlideUtil {


    public static void load(Object res, ImageView imageView) {
        load(res, imageView, 0);
    }


    public static void load(Object res, ImageView imageView, @DrawableRes int defaultRes) {
        RequestBuilder requestBuilder =
                Glide.with(MyApplication.context)
                        .load(res)
                        .fitCenter();

        if (defaultRes != 0) {
            requestBuilder.error(defaultRes);
        }

        requestBuilder.into(imageView);

    }


    public static void loadfitCenter(Object res, ImageView imageView) {
        RequestBuilder requestBuilder =
                Glide.with(MyApplication.context)
                        .load(res)
                        .fitCenter();
        requestBuilder.into(imageView);
    }


    /**
     * @param url
     * @param imageView
     */
    public static void loadGif(Object url, ImageView imageView) {
        Glide.with(MyApplication.context)
                .asGif()
                .load(url)
                .into(imageView);
    }


    public static void loadRoundCircle(Object res, ImageView imageView) {
        loadRoundCircle(res, imageView, R.mipmap.ic_default_pro);
    }

    public static void loadRoundCircle(Object res, ImageView imageView, @DrawableRes int defaultRes) {
        Glide.with(MyApplication.context)
                .load(res)
                .centerCrop()
                .transform(new CircleCrop())
                .error(defaultRes)
                .into(imageView);
    }


    public enum RoundType {
        Top,
        RIGHT,
        All,
        Round
    }

    public enum ShowType {
        CenterCrop,
        FitCenter
    }


    /**
     * 加载圆角图片
     *
     * @param url       图片资源
     * @param imageView 控件
     */
    public static void load(Object url, ImageView imageView, int radius, RoundType roundType) {
        RoundedCornersTransformation.CornerType cornerType;
        if (radius > 0) {
            if (roundType == RoundType.Top) {
                cornerType = RoundedCornersTransformation.CornerType.TOP;
            } else if (roundType == RoundType.RIGHT) {
                cornerType = RoundedCornersTransformation.CornerType.RIGHT;
            } else {
                cornerType = RoundedCornersTransformation.CornerType.ALL;
            }

            Glide.with(MyApplication.context)
                    .load(url)
                    .transform(new RoundedCornersTransformation(radius, 0, cornerType))
                    .into(imageView);
        } else {
            load(url, imageView);
        }
    }

    public static void loadCeterCrop(Object url, ImageView imageView, int radius, RoundType roundType) {

        RoundedCornersTransformation.CornerType cornerType;
        if (radius > 0) {
            if (roundType == RoundType.Top) {
                cornerType = RoundedCornersTransformation.CornerType.TOP;
            } else if (roundType == RoundType.RIGHT) {
                cornerType = RoundedCornersTransformation.CornerType.RIGHT;
            } else {
                cornerType = RoundedCornersTransformation.CornerType.ALL;
            }

            MultiTransformation multi = new MultiTransformation<>(
                    new CenterCrop(),
                    new RoundedCornersTransformation(radius, 0, cornerType));

            Glide.with(MyApplication.context)
                    .load(url)
                    .transform(multi)
                    .into(imageView);
        } else {
            load(url, imageView);
        }


    }


    /**
     * 图片圆角配置
     *
     * @param roundingRadius 圆角大小
     * @return
     */
    private static RequestOptions getRoundedCornersOptions(int roundingRadius, RoundedCornersTransformation.CornerType cornerType) {
        RequestOptions sharedOptions =
                new RequestOptions()
                        .transform(new RoundedCornersTransformation(roundingRadius, 0, cornerType))
                        .error(R.mipmap.ic_default_pro);
        return sharedOptions;
    }


}
